package br.ufpe.cin.rdfilter.util;

import java.util.ArrayList;
import java.util.List;

import br.ufpe.cin.rdfilter.feedback.Annotation;
import br.ufpe.cin.rdfilter.feedback.Feedback;
import br.ufpe.cin.rdfilter.feedback.Triple;
import br.ufpe.cin.rdfilter.model.Manager;

public class FeedbackInference {
	int count =0;
	List<Annotation> annot = new ArrayList<Annotation>();
	List<Triple> triple = new ArrayList<Triple>();
	List<String> vars = new ArrayList<String>();
	List<String> terms = new ArrayList<String>();
	Manager m;
	public FeedbackInference(List<Annotation> annot, List<Triple> triple){
		this.annot=annot;
		this.triple=triple;
	}
	
	public FeedbackInference(List<Annotation> annot){
		this.annot=annot;
	}
	
	public FeedbackInference(Manager m){
		this.m=m;
	}
		
	/**
	 * Infer new annotations from initial feedback and results of query
	 * @return
	 */
	public List<br.ufpe.cin.rdfilter.model.Feedback> setInference(){
		int i=0;
		int count = 0;
		Boolean signal=false;
		String temp="";
		List<br.ufpe.cin.rdfilter.model.Feedback> feedbackList = m.getFeedback();
		List<br.ufpe.cin.rdfilter.model.Feedback> f_temp = new ArrayList<br.ufpe.cin.rdfilter.model.Feedback>();
		br.ufpe.cin.rdfilter.model.Feedback feedback_temp = new br.ufpe.cin.rdfilter.model.Feedback();
		while(i<feedbackList.size()){
			signal=false;
			int j=0;

			while(j<terms.size() && !signal){
				temp=feedbackList.get(i).getTerm();
				if(StringSimilarity.isSimilarity(temp, terms.get(j), 0.90)){
					if(feedbackList.get(i).getType().equals("True Positive") ||
							feedbackList.get(i).getType().equals("False Negative")){
						//add new annotation with TP
						feedback_temp=feedbackList.get(i);
						feedback_temp.setType("True Positive");
						f_temp.add(feedback_temp);
					}
					//false positive
					else{
							//add new annotation with FP
						feedback_temp=feedbackList.get(i);
						feedback_temp.setType("False Positive");
						f_temp.add(feedback_temp);
					}
					
					signal=true;
				}
				j++;
			}
			if(!signal && feedbackList.get(i).getType().equals("False Negative")) count++;
			i++;
		}
		return f_temp;

	}

	public Feedback setInference2(){
		String query = annot.get(0).getIdQuery();
		List<Annotation> a = new ArrayList<Annotation>();
		Boolean signal=false;
		count = 0;
		int element=0;
		int i=0;
		String temp="";
		while(i<annot.size()){
			signal=false;
			int j=0;

			while(j<vars.size() && !signal){
				temp="";
//				for(int k=0;k<2;k++){ //comentado no dia 19 para efeito de testes... (numero de colunas da tripla)
//					temp += annot.get(i).getTerms().get(k)+";";
//				}
//				temp=temp.substring(0, temp.length()-1);
				
				//adicionado no lugar do codigo acima
				temp = annot.get(i).getTerms().get(0);
				
				if(StringSimilarity.isSimilarity(temp, vars.get(j), 0.85)){
//				if (temp.equals(vars.get(j))){
					if(annot.get(i).getType().equals("True Positive") ||
							annot.get(i).getType().equals("False Negative")){
						//add new annotation with TP
						a.add(annot.get(i));
						a.get(element).setType("True Positive");
					}
					//false positive
					else{
							//add new annotation with FP
						a.add(annot.get(i));
						a.get(element).setType("False Positive");
					}
					element++;
					signal=true;
				}
				j++;
			}
			if(!signal && annot.get(i).getType().equals("False Negative")) count++;
			i++;
		}
		Feedback f = new Feedback(query, a);
		f.setFn(count);
		f.recalculate();
		return f;
	}
	
	public List<String> getVars() {
		return vars;
	}

	public void setVars(List<String> vars) {
		this.vars = vars;
	}

	public List<String> getTerms() {
		return terms;
	}

	public void setTerms(List<String> terms) {
		this.terms = terms;
	}
	
	public int getCount(){
		return count;
	}
	
}